package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumReplyBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.MyAdapter;
import cn.bocweb.spinning.view.forun.ForunDetailActivity;

/**
 * Created by chenchangxing on 16/4/8.
 */
public class ReplyAdapter extends MyAdapter<ForumReplyBean.DataBean.ReplyBean> {

    String forumid;

    public ReplyAdapter(Context context, List<ForumReplyBean.DataBean.ReplyBean> list, String forumid) {
        super(context, list);
        this.forumid = forumid;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_reply_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mReplyNickname.setText(list.get(position).getNickname());
        holder.mContent.setText(list.get(position).getMyReply());
        holder.mReplyContent.setText(list.get(position).getContent());
        if (list.get(position).getLocation().isEmpty()) {
            holder.mAddress.setText("火星");
        } else {
            holder.mAddress.setText(list.get(position).getLocation());
        }
        holder.mTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(list.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));
        holder.mBtnResponseMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ForunDetailActivity.class);
                intent.putExtra(Constant.REPLY_TO_FORUN_DETAIL_BEAN, list.get(position));
                intent.putExtra(Constant.REPLY_TO_FORUN_DETAIL_FORUM, forumid);
                context.startActivity(intent);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.icon)
        ImageView mIcon;
        @Bind(R.id.nickname)
        TextView mNickname;
        @Bind(R.id.position)
        TextView mPosition;
        @Bind(R.id.reply_nickname)
        TextView mReplyNickname;
        @Bind(R.id.reply_content)
        TextView mReplyContent;
        @Bind(R.id.layout)
        LinearLayout mLayout;
        @Bind(R.id.content)
        TextView mContent;
        @Bind(R.id.time)
        TextView mTime;
        @Bind(R.id.address)
        TextView mAddress;
        @Bind(R.id.btn_response_msg)
        LinearLayout mBtnResponseMsg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
